<?php
namespace Admin\Controller;
use Think\Controller;
class AdminUserController extends AdminController {
    public function index(){
        $AdminUser = M('AdminUser');//实例化AdminUser对象
        $count = $AdminUser->count();// 查询满足要求的总记录数
        $itemNum = 6; //每页显示的记录数
        $show = c_page($count);

        //page方法分页
        $data = $AdminUser->order('u_time desc, id desc')
                ->page(I('get.p',1) . ',' . $itemNum)
                ->select();
        if(IS_AJAX){
            $json['data'] = $data;
            $json['page'] = $show;
            $this->ajaxReturn($json);
        }else{
            $this->assign('title','管理员列表');//标题
            $this->assign('data',$data);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
        }
    }


    public function add(){
        if(IS_POST){
            $data = I('post.');
//            exit(dump($data));
            $AdminUser = D('AdminUser');
            $AdminUser->setData($data);
            $res = $AdminUser->insertDb();
            if($res){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败：'.$AdminUser->getError());
            }
        }else{
            $this->assign('title','管理员添加');//标题
            $this->display();
        }
    }


    //$id 表示URL问号后面的id字段
    public function edit($id = 0){
        if(IS_POST){
            $data = I('post.');
            $AdminUser = D('AdminUser');
            $AdminUser->setData($data);
            $res = $AdminUser->updateDb();
            if($res){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败：'.$AdminUser->getError());
            }
        }else{
            $data = M('AdminUser')->find($id);
            $this->assign('data',$data);
            $this->assign('title','管理员修改');//标题
            $this->display();
        }
    }

    public function delete($id){
        if($id<1){
            $this->error('参数错误,至少选择一条数据');
        }
        if(!is_array($id)){
            $res = M('AdminUser')->delete($id);
        }else{
            $wh['id'] = array('in',implode(',', $id));
            $res = M('AdminUser')->where($wh)->delete();
        }
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败：',U('index'));
        }
    }


}
?>